{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "import PIL.Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 500\n",
      "+---------+\n",
      "|\u001B[34;1mR\u001B[0m: | : :G|\n",
      "| : | :\u001B[43m \u001B[0m: |\n",
      "| : : : : |\n",
      "| | : | : |\n",
      "|Y| : |\u001B[35mB\u001B[0m: |\n",
      "+---------+\n",
      "\n",
      "163\n",
      "+---------+\n",
      "|\u001B[34;1mR\u001B[0m: | : :G|\n",
      "| : |\u001B[43m \u001B[0m: : |\n",
      "| : : : : |\n",
      "| | : | : |\n",
      "|Y| : |\u001B[35mB\u001B[0m: |\n",
      "+---------+\n",
      "  (West)\n",
      "163\n"
     ]
    }
   ],
   "source": [
    "env = gym.make('Taxi-v3')\n",
    "print(env.action_space.n, env.observation_space.n)\n",
    "env.reset()\n",
    "# PIL.Image.fromarray(env.render())\n",
    "state = env.reset()\n",
    "for t in range(2):\n",
    "    env.render()\n",
    "    print(state)\n",
    "    action = env.action_space.sample()\n",
    "    observation, reward, done, info = env.step(action)\n",
    "    if done:\n",
    "        print(\"Episode finished after {} timesteps\".format(t+1))\n",
    "        break\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 (4,)\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n",
      "[ 0.04033982 -0.02393146 -0.04583971 -0.00210756]\n"
     ]
    }
   ],
   "source": [
    "env = gym.make('CartPole-v1')       # 实例化一个游戏环境，参数为游戏名称\n",
    "print(env.action_space.n, env.observation_space.shape)\n",
    "state = env.reset()     \n",
    "for _ in range(10):\n",
    "    env.render()\n",
    "    print(state)\n",
    "    action = env.action_space.sample()\n",
    "    observation, reward, done, info = env.step(action)\n",
    "    \n",
    "    if done:\n",
    "        print(\"Episode finished after {} timesteps\".format(t+1))\n",
    "        break\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 (210, 160, 3)\n",
      "['NOOP', 'UP', 'RIGHT', 'LEFT', 'DOWN', 'UPRIGHT', 'UPLEFT', 'DOWNRIGHT', 'DOWNLEFT']\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n"
     ]
    }
   ],
   "source": [
    "env = gym.make('MsPacman-v0')\n",
    "print(env.action_space.n, env.observation_space.shape)\n",
    "print(env.get_action_meanings())\n",
    "state = env.reset()\n",
    "for _ in range(10):\n",
    "    env.render()\n",
    "    print(state.shape)\n",
    "    action = env.action_space.sample()\n",
    "    observation, reward, done, info = env.step(action)\n",
    "    \n",
    "    if done:\n",
    "        print(\"Episode finished after {} timesteps\".format(t+1))\n",
    "        break\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 (210, 160, 3)\n",
      "['NOOP', 'FIRE', 'RIGHT', 'LEFT']\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n",
      "(210, 160, 3)\n"
     ]
    }
   ],
   "source": [
    "env = gym.make('Breakout-v0')\n",
    "print(env.action_space.n, env.observation_space.shape)\n",
    "print(env.get_action_meanings())\n",
    "state = env.reset()\n",
    "for _ in range(10):\n",
    "    env.render()\n",
    "    print(state.shape)\n",
    "    action = env.action_space.sample()\n",
    "    observation, reward, done, info = env.step(action)\n",
    "    \n",
    "    if done:\n",
    "        print(\"Episode finished after {} timesteps\".format(t+1))\n",
    "        break\n",
    "env.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}